package t10086.WorkThread.Shared.Client;


import t10086.Data.Robot.RobotType;
import t10086.Model.Messages.Message;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


public class Client implements Serializable {
    private InetSocketAddress socketAddress;
    private Socket socket;
    private String  serialNumber;
    private RobotType Role;// robot deliver
    private BlockingQueue<Message> senderQueue;//发送线程的消息队列

    public synchronized void close(){
        try {
            socket.close();
        } catch (IOException e) {
           System.out.println("Error closing socket");
            return;
        }
        System.out.printf("Client for %s[%s] address:[%s] closed\n",Role,serialNumber,socketAddress);
    }

    public synchronized void putMessage(Message message) {
        try {
            senderQueue.put(message);
        } catch (InterruptedException e) {
            System.out.printf("sender for %s[%s] is interrupted\n",Role,serialNumber);
        }
    }
    public Client(InetSocketAddress socketAddress, Socket socket) {
        this.socketAddress = socketAddress;
        this.socket = socket;
        this.senderQueue = new LinkedBlockingQueue<>();
    }
    public Client(){}

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    public InetSocketAddress getSocketAddress() {
        return socketAddress;
    }

    public void setSocketAddress(InetSocketAddress socketAddress) {
        this.socketAddress = socketAddress;
    }

    public RobotType getRole() {
        return Role;
    }

    public void setRole(RobotType role) {
        Role = role;
    }

    public void setSenderQueue(BlockingQueue<Message> senderQueue) {
        this.senderQueue = senderQueue;
    }
    public BlockingQueue<Message> getSenderQueue() {
        return senderQueue;
    }
}
